package com.gy.spark.core

import org.apache.spark.{SparkConf, SparkContext}

object MainBroadcast {
  def main(args: Array[String]): Unit = {

//    broadcast()
    noBroadcast()

  }

  def broadcast(): Unit = {
    val conf = new SparkConf().setMaster("local[2]").setAppName("brocast")
    val sc = new SparkContext(conf)
    val rdd1 = sc.textFile("words.txt")
    val word = "INFO"
    //通过调用sparkContext对象的broadcast方法把数据广播出去
    val broadCast = sc.broadcast(word)

    //在executor中通过调用广播变量的value属性获取广播变量的值
    val rdd2 = rdd1.flatMap(_.split(" ")).filter(x => x.equals(broadCast.value))
    rdd2.foreach(x => println(x))

    sc.stop
  }

  def noBroadcast(): Unit = {
    val conf = new SparkConf().setMaster("local[2]").setAppName("brocast")
    val sc = new SparkContext(conf)
    val rdd1 = sc.textFile("words.txt")
    val word = "INFO"
    //在executor中通过调用广播变量的value属性获取广播变量的值
    val rdd2 = rdd1.flatMap(_.split(" ")).filter(x => x.equals(word))
    rdd2.foreach(x => println(x))

    sc.stop
  }

}
